package com.project.weather.common;


import com.alibaba.druid.util.StringUtils;

import java.io.Serializable;

public class AResultCode implements Serializable {
    private static final long serialVersionUID = -2893315045048435214L;
    private String code;
    private String msg;


    //登录相关
    public static final AResultCode LOGINERROR = new AResultCode("410", "用户名或密码错误");
    public static final AResultCode LOGINCODEERROR = new AResultCode("411", "验证码错误");
    public static final AResultCode NOTAUTH = new AResultCode("401", "未认证");




    public static final AResultCode OK = new AResultCode("0", "操作成功");
    public static final AResultCode FAIL = new AResultCode("0-001", "操作失败");
    public static final AResultCode UNDEFINED = new AResultCode("0-002", "未知错误");
    public static final AResultCode IS_NULL = new AResultCode("0-003", "对象为空");
    public static final AResultCode MISS_ID = new AResultCode("0-004", "传入主键为空");
    public static final AResultCode UNEXIST = new AResultCode("0-005", "查询数据为空");
    public static final AResultCode EXIST = new AResultCode("0-006", "数据已存在");
    public static final AResultCode EXCEPTION = new AResultCode("0-007", "系统异常，操作失败");
    public static final AResultCode AUTH_INFO_MISS = new AResultCode("0-008", "数据校验失败");
    public static final AResultCode STATUS_404 = new AResultCode("0-009", "404,接口资源未找到");
    public static final AResultCode FALLBACK = new AResultCode("0-010", "远程调用异常，服务降级处理");
    public static final AResultCode RECODE = new AResultCode("0-011", "编码重复");
    public static final AResultCode INVAILD_AUTHORIZATION = new AResultCode("0-012", "认证失败，非法请求");
    public static final AResultCode INVALID_CRON = new AResultCode("0-013", "无效的cron表达式");
    public static final AResultCode FILE_ERROR = new AResultCode("0-014", "文件格式错误，请检查");
    public static final AResultCode WRONG_PASSWORD = new AResultCode("0-015", "密码错误");
    public static final AResultCode WRONG_USERNAME_OR_PASSWORD = new AResultCode("0-016", "账号或密码错误");
    public static final AResultCode WRONG_USERNAME = new AResultCode("0-017", "该账号不存在");
    public static final AResultCode UPLOAD_FILE_IMAGE_ANALYZE_ERROR = new AResultCode("0-018", "服务端解析文件出错");
    public static final AResultCode UPLOAD_FILE_IMAGE_NOT_QUALIFIED = new AResultCode("0-019", "图片不合法");
    public static final AResultCode AREADY_EXIST_USERNAME = new AResultCode("0-020", "用户名已存在");
    public static final AResultCode WRONG_FORBIDDEN = new AResultCode("0-021", "账号被禁用，请联系管理员");


    public AResultCode() {
    }

    public AResultCode(String code) {
        this.code = code;
    }

    public AResultCode(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static AResultCode getCode(AResultCode resultCode, String msg) {
        if (resultCode != null) {
            AResultCode rc = new AResultCode(resultCode.code, msg);
            resultCode = rc;
        } else {
            resultCode = UNDEFINED;
        }

        return resultCode;
    }

    public static AResultCode getExceptionCode(Throwable e) {
        StringBuffer msg = new StringBuffer("系统异常，操作失败");
        StackTraceElement[] messages = e.getStackTrace();
        int length = messages.length;
        if (e.getCause() != null) {
            String message = e.getCause().getMessage();
            if (message != null) {
                message = message.replace("\"", "").replace("'", "");
            }

            msg.append("cause:" + e.getCause().getClass() + "," + message);
        } else {
            for(int i = 0; i < (length > 1 ? 1 : length); ++i) {
                if (i == 0) {
                    msg.setLength(0);
                }

                msg.append(" cause: " + e.getClass().getName() + "  " + e.getMessage() + "\\n");
                msg.append(" detail: " + messages[i].toString() + "\\n ");
            }
        }

        return getCode(EXCEPTION, msg.toString());
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int hashCode() {
        int result = 1;
        result = 31 * result + (this.code == null ? 0 : this.code.hashCode());
        result = 31 * result + (this.msg == null ? 0 : this.msg.hashCode());
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (this.getClass() != obj.getClass()) {
            return false;
        } else {
            AResultCode other = (AResultCode)obj;
            if (this.code == null) {
                if (other.code != null) {
                    return false;
                }
            } else if (!this.code.equals(other.code)) {
                return false;
            }

            if (this.msg == null) {
                return other.msg == null;
            } else {
                return this.msg.equals(other.msg);
            }
        }
    }

    public String toString() {
        return "[code:" + this.code + ",msg:" + this.msg + "]";
    }

    public boolean isOk() {
        return this.isOk(OK.code);
    }

    public boolean isOk(String expectedCode) {
        return StringUtils.equals(expectedCode, this.code);
    }

    public static boolean isOk(ResultMap<AResultCode, ?> result) {
        return result != null && result.getCode() != null && ((AResultCode)result.getCode()).isOk();
    }

    public static boolean isFailed(ResultMap<AResultCode, ?> result) {
        return !isOk(result);
    }

    public static boolean isOk(ResultMap<AResultCode, ?> result, String expectedCode) {
        return result != null && result.getCode() != null && ((AResultCode)result.getCode()).isOk(expectedCode);
    }
}
